'''
Created on 18.11.2012

@author: B. Wilbertz
'''
def xstr(s):
    return '' if s is None else str(s)

def xfloat(x):
    return float('nan') if x is None else x

def msec2kmh(x):
    return None if x is None else x * 3.6

def readTZfromFilesystem():
    with file('/etc/timezone') as f:
        tz = f.readline().rstrip()
        
    return "GMT" if tz is None else tz 

def isotime2local(isotime):
    import datetime
    from PyCoiGlobals import tz_local, tz_utc
    dt = datetime.datetime.strptime(isotime[:19], '%Y-%m-%dT%H:%M:%S')
    utc_dt = tz_utc.localize(dt)
    return utc_dt.astimezone(tz_local).strftime("%H:%M:%S")


def isotime_diff(time1, time2):
    import time
    if time1 is None or time2 is None:
        return 0
    else:
        try:
            return time.mktime(time.strptime(time2[:19], '%Y-%m-%dT%H:%M:%S')) \
             - time.mktime(time.strptime(time1[:19], '%Y-%m-%dT%H:%M:%S')) \
             + ( float(time2[20:23]) - float(time1[20:23])  ) / 1000.0
        except:
            return 0
        
def isSpeedValid(speedInMperSec):
    from PyCoiGlobals import minimalValidSpeedInMperSec
    return False if speedInMperSec is None else speedInMperSec > minimalValidSpeedInMperSec

def nonLinearFactor(speed):
    return pow(max(speed / 70, 0.0), 1.5) 